/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.suning.utils;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.QueryShardContext;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

public class QueryStringJSONBuilder extends AbstractQueryBuilder<QueryStringJSONBuilder> {

    private JSONObject jsonObject;

    public QueryStringJSONBuilder(JSONObject jsonObject) {
        this.jsonObject = jsonObject;
    }

    public JSONObject getJsonObject() {
        return jsonObject;
    }


    /**
     * {
     *     "query":{
     *         "filtered":{
     *             "query":{
     *                 "match":{
     *                     "name":"linjian"
     *                 }
     *             }
     *         }
     *     }
     * }
     * builder.start(match)
     * builder.field("name",linjian)
     * builder.field(query,builder)
     * 递归构造builder
     * @param builder
     * @param object
     */
    public void builderAsJsonObject(XContentBuilder builder, JSONObject object) throws IOException {

        Iterator iterator = object.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = String.valueOf(entry.getKey());
            Object value = entry.getValue();
            if (value instanceof  JSONObject && JSONUtils.mayBeJSON(String.valueOf(value))) {
                builder.startObject(key);
                builderAsJsonObject(builder,JSONObject.fromObject(value));
                builder.endObject();
            }else{
                builder.field(key, value);
            }
        }
    }

    @Override
    protected void doWriteTo(StreamOutput streamOutput) throws IOException {

    }

    @Override
    protected void doXContent(XContentBuilder xContentBuilder, Params params) throws IOException {
        if (JSONUtils.isNull(jsonObject)) {
            return ;
        }
        builderAsJsonObject(xContentBuilder,jsonObject);
    }

    @Override
    protected Query doToQuery(QueryShardContext queryShardContext) throws IOException {
        return null;
    }

    @Override
    protected boolean doEquals(QueryStringJSONBuilder queryStringJSONBuilder) {

        return true;
    }

    @Override
    protected int doHashCode() {
        return 0;
    }

    public String getWriteableName() {
        return "match_all";
    }
}
